:: Required Updates to MSI
::
:: After building the DaveSexton.AutoInputProtection.Installer Setup and Deployment project, this script updates the .msi package for distribution.
:: 
:: Remember to run the verification tool in Orca and include any required error fixes here.  If necessary, rebuild the MSI.
:: 
:: NOTE: WiRunSQL.vbs has been modified to support double-quotes using a replacement token: |
::			 Each instance of | is replaced with one double-quote character instead: "
:: 
@ECHO OFF

SET MSI=%~f1
SET SCRIPT=SCRIPT%2

IF %SCRIPT% NEQ SCRIPT (
	CALL :SQL %2
	EXIT
)

:: MSI Online Documentation:  http://msdn2.microsoft.com/en-us/library/aa369398.aspx

:: -- EulaForm Dialog Control Event Updates --

:: Change the event order from 1 to 2 for the Next button's NewDialog event (event 0 is the EndDialog event) and ensure that it only occurs
:: if either of the VS2005 or VS2008 appsearch properties have been satisfied.  Since the Condition column is part of a primary key, 
:: the entire row must be deleted and reinserted with the updates.
CALL :SQL "DELETE FROM ControlEvent WHERE Dialog_='EulaForm' AND Control_='NextButton' AND Event='NewDialog'"
IF %ERRORLEVEL% NEQ 0 EXIT

CALL :SQL "INSERT INTO ControlEvent (Dialog_,Control_,Event,Argument,Condition,Ordering) VALUES ('EulaForm','NextButton','NewDialog','[EulaForm_NextArgs]','EulaForm_NextArgs<>|| AND (VS2005 OR VS2008)',2)"
IF %ERRORLEVEL% NEQ 0 EXIT

:: Insert Event 1 for the Next button:
:: No more custom dialogs are shown if neither of the VS2005 or VS2008 appsearch properties have been satisfied.
CALL :SQL "INSERT INTO ControlEvent (Dialog_,Control_,Event,Argument,Condition,Ordering) VALUES ('EulaForm','NextButton','EndDialog','Return','EulaForm_NextArgs<>|| AND NOT VS2005 AND NOT VS2008',1)"
IF %ERRORLEVEL% NEQ 0 EXIT

:: -- CustomCheckA Control Conditions Updates --

:: Hide and disable Checkbox1 if the VS2005 appsearch property has not been satisfied.  (Both are required to prevent shortcut key access.)
CALL :SQL "INSERT INTO ControlCondition (Dialog_,Control_,Action,Condition) VALUES ('CustomCheckA','Checkbox1','Hide','NOT VS2005')"
IF %ERRORLEVEL% NEQ 0 EXIT

CALL :SQL "INSERT INTO ControlCondition (Dialog_,Control_,Action,Condition) VALUES ('CustomCheckA','Checkbox1','Disable','NOT VS2005')"
IF %ERRORLEVEL% NEQ 0 EXIT

:: Hide and disable Checkbox2 if the VS2008 appsearch property has not been satisfied.  (Both are required to prevent shortcut key access.)
CALL :SQL "INSERT INTO ControlCondition (Dialog_,Control_,Action,Condition) VALUES ('CustomCheckA','Checkbox2','Hide','NOT VS2008')"
IF %ERRORLEVEL% NEQ 0 EXIT

CALL :SQL "INSERT INTO ControlCondition (Dialog_,Control_,Action,Condition) VALUES ('CustomCheckA','Checkbox2','Disable','NOT VS2008')"
IF %ERRORLEVEL% NEQ 0 EXIT

:: -- CustomCheckA Dialog Control Event Updates --

:: Change the event order from 1 to 4 for the Next button's NewDialog event (event 0 is the EndDialog event) and ensure that it only occurs
:: if the user has selected toolbox installation for at least one version of VS.  Since the Condition column is part of a primary key, 
:: the entire row must be deleted and reinserted with the updates.
CALL :SQL "DELETE FROM ControlEvent WHERE Dialog_='CustomCheckA' AND Control_='Next' AND Event='NewDialog'"
IF %ERRORLEVEL% NEQ 0 EXIT

CALL :SQL "INSERT INTO ControlEvent (Dialog_,Control_,Event,Argument,Condition,Ordering) VALUES ('CustomCheckA','Next','NewDialog','[CustomCheckA_NextArgs]','CustomCheckA_NextArgs<>|| AND (VS2005REGCONTROL=1 OR VS2008REGCONTROL=1)',4)"
IF %ERRORLEVEL% NEQ 0 EXIT

:: Insert Event 1 for the Next button:
:: No more custom dialogs are shown if no Visual Studio versions have been chosen for toolbox installation.
CALL :SQL "INSERT INTO ControlEvent (Dialog_,Control_,Event,Argument,Condition,Ordering) VALUES ('CustomCheckA','Next','EndDialog','Return','VS2005REGCONTROL<>1 AND VS2008REGCONTROL<>1',1)"
IF %ERRORLEVEL% NEQ 0 EXIT

:: Insert Event 2 for the Next button:
:: This new event will assign the argument for the Next button's NewDialog event to the name of the dialog that allows the user to choose the VS 2005 controls path.
:: The condition is that the user must have selected the VS 2005 check box.
CALL :SQL "INSERT INTO ControlEvent (Dialog_,Control_,Event,Argument,Condition,Ordering) VALUES ('CustomCheckA','Next','[CustomCheckA_NextArgs]','CustomTextA','CustomCheckA_NextArgs<>|| AND VS2005REGCONTROL=1',2)"
IF %ERRORLEVEL% NEQ 0 EXIT

:: Insert Event 3 for the Next button:
:: This new event will assign the argument for the Next button's NewDialog event to the name of the dialog that allows the user to choose the VS 2008 controls path.
:: The condition is that the user must not have selected the VS 2005 check box and must have selected the VS 2008 check box.
CALL :SQL "INSERT INTO ControlEvent (Dialog_,Control_,Event,Argument,Condition,Ordering) VALUES ('CustomCheckA','Next','[CustomCheckA_NextArgs]','CustomTextB','CustomCheckA_NextArgs<>|| AND VS2005REGCONTROL<>1 AND VS2008REGCONTROL=1',3)"
IF %ERRORLEVEL% NEQ 0 EXIT


:: -- CustomTextA Dialog Control Event Updates --

:: Change the event order from 1 to 2 for the NextButton's NewDialog event (event 0 is the EndDialog event) and ensure that it only occurs 
:: if the user has selected VS 2008 toolbox installation.  Since the Condition column is part of a primary key, the entire row must be 
:: deleted and reinserted with the updates.
CALL :SQL "DELETE FROM ControlEvent WHERE Dialog_='CustomTextA' AND Control_='NextButton' AND Event='NewDialog'"
IF %ERRORLEVEL% NEQ 0 EXIT

CALL :SQL "INSERT INTO ControlEvent (Dialog_,Control_,Event,Argument,Condition,Ordering) VALUES ('CustomTextA','NextButton','NewDialog','[CustomTextA_NextArgs]','CustomTextA_NextArgs<>|| AND VS2008REGCONTROL=1',2)"
IF %ERRORLEVEL% NEQ 0 EXIT

:: Insert Event 1 for NextButton:
:: This new event will prevent the remaining custom dialogs from being shown if the user has not selected VS 2008 toolbox installation.
CALL :SQL "INSERT INTO ControlEvent (Dialog_,Control_,Event,Argument,Condition,Ordering) VALUES ('CustomTextA','NextButton','EndDialog','Return','VS2008REGCONTROL<>1',1)"
IF %ERRORLEVEL% NEQ 0 EXIT


:: -- CustomTextB Dialog Control Event Updates --

:: Change the event order from 0 to 2 for the PreviousButton's NewDialog event.
CALL :SQL "UPDATE ControlEvent SET Ordering = 2 WHERE Dialog_='CustomTextB' and Control_='PreviousButton' AND Event='NewDialog'"
IF %ERRORLEVEL% NEQ 0 EXIT

:: Insert Event 0 for PreviousButton:
:: This new event will assign the argument for the PreviousButton's NewDialog event to the name of the dialog that allows the user to choose the VS 2005 controls path.
:: The condition is that the user must have selected the VS 2005 check box.
CALL :SQL "INSERT INTO ControlEvent (Dialog_,Control_,Event,Argument,Condition,Ordering) VALUES ('CustomTextB','PreviousButton','[CustomTextB_PrevArgs]','CustomTextA','CustomTextB_PrevArgs<>|| AND VS2005REGCONTROL=1',0)"
IF %ERRORLEVEL% NEQ 0 EXIT

:: Insert Event 1 for PreviousButton:
:: This new event will assign the argument for the PreviousButton's NewDialog event to the name of the dialog that allows the user to choose the versions of 
:: Visual Studio for toolbox installation.
:: The condition is that the user must not have selected the VS 2005 check box.
CALL :SQL "INSERT INTO ControlEvent (Dialog_,Control_,Event,Argument,Condition,Ordering) VALUES ('CustomTextB','PreviousButton','[CustomTextB_PrevArgs]','CustomCheckA','CustomTextB_PrevArgs<>|| AND VS2005REGCONTROL<>1',1)"
IF %ERRORLEVEL% NEQ 0 EXIT


::Bug Fixes

CALL :SQL "DELETE FROM AdvtExecuteSequence WHERE AdvtExecuteSequence.`Action`='MsiUnpublishAssemblies'"
IF %ERRORLEVEL% NEQ 0 EXIT

EXIT
:SQL
CScript /Nologo WiRunSQL.vbs "%MSI%" %*
